package com.it.system.searchservice.config;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ElasticsearchConfig {

    // 主机
    private String host;
    // 端口
    private Integer port;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        if (host == null || port == null) {
            throw new IllegalStateException("Elasticsearch host or port is not configured.");
        }
        return new RestHighLevelClient(RestClient.builder(new HttpHost(host, port)));
    }
}